package com.cooper.controllers.dev;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.controllers.LoginRequired;
import com.cooper.controllers.admin.AdminRequired;
import com.cooper.core.JsonResult;
import com.cooper.entity.SchedulerInfo;
import com.cooper.entity.UserInfo;
import com.cooper.service.core.SchedulerService;
import com.cooper.util.Controllers;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

@AdminRequired
@LoginRequired
public class SchedulerController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SchedulerService scheduelrService;
	
	@Get @Post
	public String execute(Invocation inv){
		inv.addModel("entryList", this.scheduelrService.queryForList());
		return "SchedulerMain";
	}
	
	@Post("refreshAll")
	public Object refreshAll(Invocation inv){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			this.scheduelrService.refreshAllTask();
			json.setSuccess(true);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Post("executeTask")
	public Object executeTask(Invocation inv, SchedulerInfo sched){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			this.scheduelrService.executeTask(sched);
			json.setSuccess(true);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}

	@Get("edit/{id:[0-9]+}")
	public String edit(Invocation inv, @Param("id") Long id){
		if(0 < id){
			inv.addModel("entry", this.scheduelrService.loadById(id));
		}
		return "SchedulerEdit";
	}
	
	@Post("save")
	public Object save(Invocation inv, SchedulerInfo sched){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			UserInfo userInfo = Controllers.getUserInfo(inv);
			Controllers.prepareEntityForDataSaving(sched, userInfo, true);
			
			String strEffect = inv.getParameter("effect");
			boolean bEffect = "1".equals(strEffect);
			if(null == sched.getId()){
				SchedulerInfo existTarget = this.scheduelrService.getSchedulerByName(sched.getJobName());
				if(null != existTarget && StringUtils.isNotEmpty(existTarget.getJobName())){
					json.setMessage("该名称的jobName已经存在，请使用其他名称");
				} else {
					Long id = this.scheduelrService.addScheduler(sched, bEffect);
					if(id > 0){
						json.setSuccess(true);
					}
				}
			} else {
				int result = this.scheduelrService.updateScheduler(sched, bEffect);
				if(result > 0){
					json.setSuccess(true);
				}
			}
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Post("delete")
	public Object delete(Invocation inv, SchedulerInfo sched){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			UserInfo userInfo = Controllers.getUserInfo(inv);
			Controllers.prepareEntityForDataSaving(sched, userInfo, true);
			String strEffect = inv.getParameter("effect");
			boolean bEffect = "1".equals(strEffect);
			this.scheduelrService.deleteScheduler(sched, bEffect);
			json.setSuccess(true);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}
	
}
